package com.test.dj;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.asao.baseware.core.DefaultApplication;
import com.bum.guru.dj.dao.SongDAO;
import com.bum.guru.exp.dj.om.Song;
import com.bum.guru.util.BaseParser;

public class LoadSong {
	public static <T> T getBean(String beanName) {
		return (T) DefaultApplication.theApplication().getBean(beanName);
	}

	static {
		DefaultApplication app = new DefaultApplication("config/application.cfg.xml");
		app.setPropertiesLocations(new String[] {
			"/config/application.properties"
		});
		app.start();

		songDAO = getBean("songDAO");
	}

	static SongDAO	songDAO;

	public static boolean load(long id) {
		String url = "http://matxac.com/Play/1/" + id + ".xml";
		System.out.println(id);
		String html = BaseParser.loadHtml(url);
		String name = BaseParser.parseContent(html, "<Title>", "</Title>");
		String track = BaseParser.parseContent(html, "<location>", "</location>");

		if (StringUtils.isNotBlank(name) && StringUtils.isNotBlank(track)) {
			try {
				System.out.println(id + " " + name + " " + track);
				songDAO.add(id, name, track);
			} catch (Exception e) {
				e.printStackTrace();
			}
			return true;
		}
		return false;
	}

	public static void loadSongs() throws Exception {
		int failCount = 0;
		List<Song> songs = songDAO.list(0, 0, 1);

		long startId = songs.get(0).getId();
		while (failCount < 10) {
			if (!load(++startId)) {
				++failCount;
			}
		}
	}

	public static void main(String[] args) throws Exception {
//		testSongs();
		loadSongs();
		System.out.println("Done!");
	}

	public static void testSongs() throws Exception {
		boolean finish = false;

		// giữa chừng: id 28145
		long id = 25294;
		long endId = 17132;
		while (!finish) {
			List<Song> songs = songDAO.list(0, id, 50);
			for (Song song : songs) {
				id = song.getId();
				System.out.println("\n" + id + " " + song.getUrl());
				String[] parts = StringUtils.split(song.getUrl().replace("http://nhac.matxac.com/", "").replace(".flv", ""), "/");
				if (parts.length < 2) {
					songDAO.del(id);
					System.out.println("Del " + song.getUrl());
				} else {
					String url = "http://www" + parts[0] + ".zippyshare.com/downloadMusic?key=" + parts[1] + "&time=" + System.currentTimeMillis();
					int code = testLink(url);
					System.out.println(code + " " + url);
					if (code == 0) {
						songDAO.del(id);
						System.out.println("Del " + song.getUrl());
					}
				}

			}
			
			if (songs.size() < 50 || id < endId) {
				finish = true;
			}
		}

	}

	public static int testLink(String link) {
		URL url = null;
		try {
			url = new URL(link);
		} catch (MalformedURLException exception) {
			exception.printStackTrace();
		}

		InputStream in = null;
		try {
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			conn.setFollowRedirects(false);
			conn.setInstanceFollowRedirects(false);
			conn.setRequestMethod("GET");
			conn.connect();

			return conn.getContentLength() / 500000;
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
				}
			}
		}
		return 0;
	}
}
